package com.atguigu.jxc.controller;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: GoodsController
 * Description:
 * Date: 2022/1/15 9:46
 *
 * @version:
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/listInventory")
    public Map<String,Object> listPage(Integer page,Integer rows, String codeOrName,Integer goodsTypeId){

        Page<Goods> goodsList = goodsService.selectPage(page,rows,codeOrName,goodsTypeId);

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();

        stringObjectHashMap.put("total",goodsList.getTotal());

        List<Goods> result = goodsList.getResult();
        for (Goods goods : result) {
            if(goods.getSaleTotal() == null){
                goods.setSaleTotal(0);
            }
        }
        stringObjectHashMap.put("rows",result);
        return stringObjectHashMap;
    }



    @PostMapping("/list")
    public Map<String,Object> getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId){
        Page<Goods> goodsList = goodsService.getGoodsList(page, rows, goodsName, goodsTypeId);


        HashMap<String, Object> stringObjectHashMap = new HashMap<>();

        stringObjectHashMap.put("total",goodsList.getTotal());

        List<Goods> result = goodsList.getResult();
        for (Goods goods : result) {
            if(goods.getSaleTotal() == null){
                goods.setSaleTotal(0);
            }
        }
        stringObjectHashMap.put("rows",result);
        return stringObjectHashMap;
    }



    @PostMapping("/save")
    public ServiceVO insertOrUpdateGoods(Goods Goods){





        try {
            goodsService.insertOrUpdateGoods(Goods);
            return new ServiceVO<>(100,"请求成功");

        }catch (Exception e){
            e.printStackTrace();
        }

        return new ServiceVO<>(101,"添加或修改失败");


    }


    @PostMapping("/delete")
    public ServiceVO deleteGoods(Integer goodsId){


        try {
            goodsService.deleteGoodsByIds(goodsId);
            return new ServiceVO<>(100,"请求成功");

        }catch (Exception e){
            e.printStackTrace();
        }

        return new ServiceVO<>(101,"删除失败");
    }



    @PostMapping("/getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        Page<Goods> goodsList = goodsService.getHasInventoryQuantity(page,rows,nameOrCode);


        HashMap<String, Object> stringObjectHashMap = new HashMap<>();

        stringObjectHashMap.put("total",goodsList.getTotal());

        List<Goods> result = goodsList.getResult();
        for (Goods goods : result) {
            if(goods.getSaleTotal() == null){
                goods.setSaleTotal(0);
            }
        }
        stringObjectHashMap.put("rows",result);
        return stringObjectHashMap;
    }


    @PostMapping("/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        Page<Goods> goodsList = goodsService.getNoInventoryQuantity(page,rows,nameOrCode);


        HashMap<String, Object> stringObjectHashMap = new HashMap<>();

        stringObjectHashMap.put("total",goodsList.getTotal());

        List<Goods> result = goodsList.getResult();
        for (Goods goods : result) {
            if(goods.getSaleTotal() == null){
                goods.setSaleTotal(0);
            }
        }
        stringObjectHashMap.put("rows",result);
        return stringObjectHashMap;
    }




    @PostMapping("/saveStock")
    public ServiceVO saveStock(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){

        try {
            goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);

            return new ServiceVO<>(100,"请求成功");

        }catch (Exception e){
            e.printStackTrace();
        }

        return new ServiceVO<>(101,"添加或修改库存失败");
    }




    @PostMapping("/deleteStock")
    public ServiceVO deleteStock(Integer goodsId){

        try {
            ServiceVO serviceVO = goodsService.deleteStock(goodsId);

            return serviceVO;

        }catch (Exception e){
            e.printStackTrace();
        }

        return new ServiceVO<>(101,"删除库存失败");
    }




    @PostMapping("/listAlarm")
    public Map<String,Object> getlistAlarm(){
        List<Goods> goodsList = goodsService.getlistAlarm();


        HashMap<String, Object> stringObjectHashMap = new HashMap<>();




        for (Goods goods : goodsList) {
            if(goods.getSaleTotal() == null){
                goods.setSaleTotal(0);
            }
        }
        stringObjectHashMap.put("rows",goodsList);
        return stringObjectHashMap;
    }


}















